package com.it.od.manual;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: liminghui
 * @date: 2024/3/27 19:46
 * @version: 1.0
 * @description:
 * 1160. 拼写单词
 * 给你一份『词汇表』（字符串数组） words 和一张『字母表』（字符串） chars。
 * 假如你可以用 chars 中的『字母』（字符）拼写出 words 中的某个『单词』（字符串），那么我们就认为你掌握了这个单词。
 * 注意：每次拼写时，chars 中的每个字母都只能用一次。
 * 返回词汇表 words 中你掌握的所有单词的 长度之和。
 * <p>
 * 示例 1：
 * 输入：words = ["cat","bt","hat","tree"], chars = "atach"
 * 输出：6
 * 解释：
 * 可以形成字符串 "cat" 和 "hat"，所以答案是 3 + 3 = 6。
 * <p>
 * 提示：
 * 1 <= words.length <= 1000
 * 1 <= words[i].length, chars.length <= 100
 * 所有字符串中都仅包含小写英文字母
 */
public class Lee116 {
    public static void main(String[] args) {
        String[] words = {"cat", "bt", "hat", "tree"};
        String chars = "atach";
        int sumOfLen = countCharacters(words, chars);
        System.out.println(sumOfLen);
    }

    public static int match(String[] words, String chars) {
        int sumOfLen = 0;
        for (String word : words) {
            // 判断words【i】能否有chars构成
            if (canSpell2(word, chars)) {
                sumOfLen += word.length();
            }
        }
        return sumOfLen;
    }

    public static int countCharacters(String[] words, String chars) {
        Map<Character, Integer> charsCnt = new HashMap<Character, Integer>();
        int length = chars.length();
        for (int i = 0; i < length; ++i) {
            char c = chars.charAt(i);
            charsCnt.put(c, charsCnt.getOrDefault(c, 0) + 1);
        }
        int ans = 0;
        for (String word : words) {
            Map<Character, Integer> wordCnt = new HashMap<Character, Integer>();
            int wordLength = word.length();
            for (int i = 0; i < wordLength; ++i) {
                char c = word.charAt(i);
                wordCnt.put(c, wordCnt.getOrDefault(c, 0) + 1);
            }
            boolean isAns = true;
            for (int i = 0; i < wordLength; ++i) {
                char c = word.charAt(i);
                if (charsCnt.getOrDefault(c, 0) < wordCnt.getOrDefault(c, 0)) {
                    isAns = false;
                    break;
                }
            }
            if (isAns) {
                ans += word.length();
            }
        }
        return ans;
    }

    private static boolean canSpell(String word, String chars) {
        Map<Character, Integer> countMap1 = new HashMap<>();
        for (int i = 0; i < word.length(); i++) {
            countMap1.put(word.charAt(i), countMap1.getOrDefault(word.charAt(i), 0) + 1);
        }
        Map<Character, Integer> countMap2 = new HashMap<>();
        for (int i = 0; i < chars.length(); i++) {
            countMap2.put(chars.charAt(i), countMap1.getOrDefault(chars.charAt(i), 0) + 1);
        }
        for (Map.Entry<Character, Integer> entry : countMap1.entrySet()) {
            if (entry.getValue() > countMap2.getOrDefault(entry.getKey(), 0)) {
                return false;
            }
        }
        return true;
    }


    // chars的次数计算可以提出来，不用每次计算
    private static boolean canSpell2(String word, String chars) {
        int[] count1 = new int[26];
        int[] count2 = new int[26];
        for (int i = 0; i < word.length(); i++) {
            count1[word.charAt(i) - 'a']++;
        }
        for (int i = 0; i < chars.length(); i++) {
            count2[chars.charAt(i) - 'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (count1[i] > count2[i]) {
                return false;
            }
        }
        return true;
    }
}
